﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using SIRC.Framework.Net;
using Shanfree.SharpMovie.Model;
using Shanfree.SharpMovie.BLL;
using Shanfree.Framework.Utility;
using System.Threading;

namespace SharpMClient
{
    public partial class TranImport : Form
    {
        public TranImport()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFileDialog.Filter = "文本文件(*.txt)|*.txt";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                tbFilepath.Text = openFileDialog.FileName;
            }
        }

        private void Importbtn_Click(object sender, EventArgs e)
        {
            try
            {
                string filePath = tbFilepath.Text;
                if (File.Exists(tbFilepath.Text) == false)
                {
                    MessageBox.Show("文件在：" + filePath + "不存在。");
                    return;
                }
                EntityXpathInfo eInfo = GetMappingFromFile();
                string urlBase = eInfo.URL;
                using (StreamReader sr = new StreamReader(filePath, true))
                {
                    while (sr.EndOfStream == false)
                    {
                        string ttCode = sr.ReadLine();
                        //http://www.imdb.com/title/tt0111161/
                        eInfo.CustomerString = ttCode.Trim();
                        string url = string.Format(urlBase, eInfo.CustomerString);
                        eInfo.URL = url;
                        Do(eInfo);

                        //ParameterizedThreadStart start = new ParameterizedThreadStart(this.Do);
                        //Thread cntThread = new Thread(start);
                        //cntThread.Start(eInfo);
                        //break;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 加载映射文档
        /// </summary>
        /// <returns></returns>
        private EntityXpathInfo GetMappingFromFile()
        {
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"EntityMapping.xml");
            EntityXpathInfo eInfo = SerializeHandler<EntityXpathInfo>.DeSerializeFromFile(filePath);
            return eInfo;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="paramStringparamparam>
        public void Do(object param)
        {
            string msg = string.Empty;
            EntityXpathInfo eInfo = param as EntityXpathInfo;
            try
            {
                try
                {
                    MovieInfo mInfo = new EntityInitialHander<MovieInfo>().GetEntityFromXpath(eInfo);
                    new Movie().Add(mInfo.IMDB_ID, mInfo.OriginalName, mInfo.EnglishName, mInfo.ChineseName, mInfo.HongkongName, mInfo.PostLink, mInfo.ProduceYear, mInfo.Director, mInfo.LeadingRule, mInfo.Introduce, mInfo.Awards, "1");
                    msg = string.Format("影片：{0}({1}) 导入成功。",mInfo.OriginalName,mInfo.IMDB_ID);
                    //Thread.Sleep(3000);
                }
                catch (MovieException me)
                {
                    msg = me.Information;
                }
                ChangeUI(msg);
            }
            catch (Exception ex)
            {
                ChangeUI(string.Format("编号{0}影片 导入失败。错误信息:{1}", eInfo.CustomerString,ex.ToString()));
            }
        }

        void ChangeUI(string message)
        {
            // 非本线程创建
            if (tbResult.InvokeRequired == true)
            {
                object[] args = new object[] { message };
                UIChangeMethod dChange = new UIChangeMethod(ChangeUI);
                tbResult.Invoke(dChange, args);
            }
            else
            {
                tbResult.Text += message + "\r\n\r\n";
            }
        }

        delegate void UIChangeMethod(string message);

    }
}
